/**
 * PluginManager.java 2008-05-16
 *        
 * Linker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.plugin;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import linker.LocalVariablesManager;
import linker.account.ManageAccount;
import linker.list.ViewList;

/**
 * The manager of plugin,in which you can select which plugin is to be initialed
 * when logined, and which not.Also you can use it to send plugin to the
 * selected user.
 * 
 * @version 2008-05-16
 * @author Jianfeng tujf.cn@gmail.com
 * 
 */

public class PluginWindow extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The table to show the plugins' status.
	 */
	private JTable table = new JTable();

	/**
	 * The tableModel to hold table's information.
	 */
	private DefaultTableModel tableModel = new DefaultTableModel();

	/**
	 * The change status button.
	 */
	private JButton changeButton = new JButton(LocalVariablesManager
			.getLocalVariables("PluginWindow.ChangeStatusButton"));

	/**
	 * The pluginManager refer.
	 */
	private static PluginWindow pluginManager;

	/**
	 * The plugin list.
	 */
	private PluginList pluginList = ViewList.getPluginList();

	/**
	 * The plugins' properties.
	 */
	private Properties plugins;

	/**
	 * 
	 * 
	 */
	public PluginWindow() {
		super(LocalVariablesManager.getLocalVariables("PluginWindow"));
		setName("PluginWindow");
		setIconImage(ManageAccount.getLogo());

		Container container = getContentPane();
		container.setLayout(new BorderLayout());

		JPanel userPanel = new JPanel();
		userPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		changeButton.addActionListener(this);
		userPanel.add(changeButton);
		container.add(userPanel, BorderLayout.NORTH);

		tableModel.addColumn(LocalVariablesManager
				.getLocalVariables("PluginWindow.TableModel.NameColumn"));
		tableModel.addColumn(LocalVariablesManager
				.getLocalVariables("PluginWindow.TableModel.StatusColumn"));
		table.setColumnSelectionAllowed(false);
		table.setCellSelectionEnabled(false);
		table.setRowSelectionAllowed(true);
		table.setModel(tableModel);
		container.add(new JScrollPane(table), BorderLayout.CENTER);

		plugins = pluginList.getPluginProperties();
		Set<String> names = plugins.stringPropertyNames();

		for (String name : names) {
			Vector<String> v = new Vector<String>();
			v.add(name);
			v.add(plugins.getProperty(name));
			tableModel.addRow(v);
		}

		addWindowListener(new WindowAdapter() {
			public void windowClosing(final WindowEvent e) {
				PluginWindow.this.dispose();
				PluginWindow.pluginManager = null;
			}
		});

		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * 
	 * @return The current pluginManager.
	 */
	public static PluginWindow getPluginManager() {
		if (pluginManager == null) {
			pluginManager = new PluginWindow();
		}

		return pluginManager;
	}

	/**
	 * @param e
	 *            ActionEvent.
	 */
	public final void actionPerformed(final ActionEvent e) {
		String disableValue = LocalVariablesManager
				.getLocalVariables("PluginWindow.TableModel.DisableValue");
		String enableValue = LocalVariablesManager
				.getLocalVariables("PluginWindow.TableModel.EnableValue");

		if (e.getSource() == changeButton) {
			int[] row = table.getSelectedRows();
			for (int i = 0; i < row.length; i++) {
				if (((String) tableModel.getValueAt(row[i], 1))
						.startsWith(disableValue)) {
					tableModel.setValueAt(enableValue, row[i], 1);
					plugins.put(tableModel.getValueAt(row[i], 0), enableValue);
					pluginList.savePluginProperties(plugins);
				} else if (((String) tableModel.getValueAt(row[i], 1))
						.startsWith(enableValue)) {
					tableModel.setValueAt(disableValue, row[i], 1);
					plugins.put(tableModel.getValueAt(row[i], 0), disableValue);
					pluginList.savePluginProperties(plugins);
				}

			}
		}
	}
}
